package com.hsy.baoinbao.modules.main.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.notice.activity.DetailNoticeActivity;
import com.hsy.baoinbao.modules.notice.adapter.NoticeAdapter;
import com.hsy.baoinbao.modules.notice.gsondata.NoticeListData;
import com.hsy.baoinbao.modules.order.activity.DetailReceivingActivity;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.jingchen.pulltorefresh.pullableview.PullableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/29.
 */
public class PublicAccementActivity extends CommonBaseActivityWithSimpleActionBar {
    private PullableListView list;
    private PullToRefreshLayout list_layout;
    private NoticeAdapter noticeAdapter;
    private List<NoticeListData.Lists> listData = new ArrayList<>();
    ;
    private NoticeListData.Result result;
    private int firstRow = 0, total = 0, current = 10;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {

            if (0 == what) {
                setNoticeList(response);
                firstRow = current + firstRow;
                CommonUtils.hideProgressDialog();
                list_layout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            if (1 == what) {

                result = DPGsonUtil.fromJson(response, NoticeListData.Result.class);
                listData.addAll(result.getLists());
                total = result.getCount();
                noticeAdapter.notifyDataSetChanged();

                firstRow = current + firstRow;
                current = current + 10;
                CommonUtils.hideProgressDialog();
                list_layout.loadmoreFinish(PullToRefreshLayout.LOAD_FINISH);
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            if (0 == what) {
                CommonUtils.hideProgressDialog();
                list_layout.refreshFinish(PullToRefreshLayout.FAIL);
            }
            if (1 == what) {
                CommonUtils.hideProgressDialog();
                list_layout.loadmoreFinish(PullToRefreshLayout.FAIL);
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_accement_list);
        list = F(R.id.list);
        list_layout = F(R.id.list_layout);

        CommonUtils.showProgressDialog(this);
        Map<String, String> params = new HashMap<>();
        params.put(Consts.TOKEN, CommonUtils.getToken(PublicAccementActivity.this));
        params.put(Consts.FIRST_ROW, String.valueOf(firstRow));
        params.put(Consts.LIST_ROWS, String.valueOf(current));

        setTitle("通知");

        HttpUtil.httpPostWithParams(PublicAccementActivity.this, 0, Consts.API_GET_NOTICE_LIST, params, callback);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(PublicAccementActivity.this, DetailNoticeActivity.class);
                intent.putExtra("id", result.getLists().get(position).getId());
                startActivity(intent);
            }
        });
        list_layout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                if (result.getLists() == null) {
                    list_layout.refreshFinish(PullToRefreshLayout.REFRESH_FINISH);
                    return;
                }
                listData.clear();
                firstRow = 0;
                current = 10;
                Map<String, String> params = new HashMap<>();
                params.put(Consts.TOKEN, CommonUtils.getToken(PublicAccementActivity.this));
                params.put(Consts.FIRST_ROW, String.valueOf(firstRow));
                params.put(Consts.LIST_ROWS, String.valueOf(current));
                HttpUtil.httpPostWithParams(PublicAccementActivity.this, 0, Consts.API_GET_NOTICE_LIST, params, callback);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                DPLogUtil.d("current=" + current);
                DPLogUtil.d("total=" + total);
                if (total == 0 || current >= total) {
                    showToast("没有更多了");
                    pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.LOAD_FINISH);
                    return;
                }
                Map<String, String> params = new HashMap<>();
                params.put(Consts.TOKEN, CommonUtils.getToken(PublicAccementActivity.this));
                params.put(Consts.FIRST_ROW, String.valueOf(firstRow));
                params.put(Consts.LIST_ROWS, String.valueOf(current));
                HttpUtil.httpPostWithParams(PublicAccementActivity.this, 1, Consts.API_GET_NOTICE_LIST, params, callback);
            }
        });
    }

    private void setNoticeList(String response) {

        result = DPGsonUtil.fromJson(response, NoticeListData.Result.class);
        total = result.getCount();
        listData = result.getLists();
        noticeAdapter = new NoticeAdapter(this, listData);
        list.setAdapter(noticeAdapter);

    }
}
